<?php

class Common_Chart_Strategy_Google_Pie extends Common_Chart_Strategy_Google {

    public function __construct() {
        parent::__construct();
    }

    public function get_chart_code() {
        return self::CHART_TYPE_PIE;
    }

    public function get_chart_type() {
        return Common_Chart::TYPE_PIE;
    }

    protected function prepare_data($arr_data, $arr_options = array()) {

        $arr_chart_data = array();

        $int_min_data = null;
        $int_max_data = null;

        $arr_series_sums = array();
        foreach($arr_data as $str_series_key => $arr) {
            $arr_series_sums[$str_series_key] = array_sum($arr);
        }

        foreach($arr_data as $str_series_key => $arr) {

            $arr_chart_data[$str_series_key] = array();

            foreach($arr as $num) {

                $mix = null;
                if(!is_null($num)) {

                    $num = ($num / $arr_series_sums[$str_series_key]) * 100;
                    $num = number_format($num, 1);

                    if(is_null($int_min_data) || ($num < $int_min_data)) {
                        $int_min_data = $num;
                    }
                    if(is_null($int_max_data) || ($num > $int_max_data)) {
                        $int_max_data = $num;
                    }

                    $mix = $num;
                }

                $arr_chart_data[$str_series_key][] = $mix;

            }
        }

        $this->set_data_min($int_min_data);
        $this->set_data_max($int_max_data);

        return $arr_chart_data;

    }

    protected function apply_data_options($arr_chart_parameters, $arr_data, $arr_options = array()) {

        $arr_chart_parameters = parent::apply_data_options($arr_chart_parameters, $arr_data, $arr_options);

        $num_maximum = $this->get_data_max();
        if($num_maximum > 100) {
            $arr_chart_parameters[self::PARAMETER_CHART_DATA_SCALE] = '0,' . $num_maximum;
        }

        return $arr_chart_parameters;
    }

    protected function apply_render_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options = array()) {

        $arr_chart_parameters = parent::apply_render_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options);
        $arr_chart_parameters = $this->apply_label_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options);

        if(isset($arr_options['3d_version'])) {
            $arr_chart_parameters[self::PARAMETER_CHART_TYPE] = self::CHART_TYPE_PIE_3D;
            $arr_chart_parameters[self::PARAMETER_CHART_MARGINS] = '30,30,30,30';
        }

        if(isset($arr_options['data_rotation'])) {
            $arr_chart_parameters[self::PARAMETER_DATA_ROTATION] = $arr_options['data_rotation'];
        }

        return $arr_chart_parameters;
    }
    
    protected function apply_colour_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options = array()) {

        if(isset($arr_options['colours'])) {

            $arr_colours = $arr_options['colours'];

            if(!isset($arr_options['colour_per_item']) || ($arr_options['colour_per_item'] == false)) {
                $arr_colours = (array) array_shift($arr_colours);
            }
            
            $str_chart_colours = implode($this->get_colour_delimiter(), $arr_colours);
            $arr_chart_parameters[self::PARAMETER_CHART_COLOUR] = $str_chart_colours;
        }

        return $arr_chart_parameters;
    }

    protected function apply_label_options($arr_chart_parameters, $arr_data, $arr_headers, $arr_options = array()) {

        if(isset($arr_options['label_format']) && is_string($arr_options['label_format'])) {

            $str_format = $arr_options['label_format'];

            $arr_series_data = reset($arr_data);

            foreach($arr_headers as $i => $str_label) {

                $str_value = $arr_series_data[$i];

                $arr_headers[$i] = str_replace(
                    array('{label}', '{value}'),
                    array($str_label, $str_value),
                    $str_format
                );
                
            }

            $str_chart_labels = implode('|', $arr_headers);
            $arr_chart_parameters[self::PARAMETER_CHART_LABELS] = $str_chart_labels;

        }

        if(isset($arr_options['legend_position'])) {
            $enm_legend_position = $this->convert_legend_position($arr_options['legend_position']);
            if(!empty($enm_legend_position)) {

                $arr_chart_parameters[self::PARAMETER_LEGEND_KEY] = $arr_chart_parameters[self::PARAMETER_CHART_LABELS];
                unset($arr_chart_parameters[self::PARAMETER_CHART_LABELS]);

                $arr_chart_parameters[self::PARAMETER_LEGEND_POSITION] = $enm_legend_position;
            }
        }

        return $arr_chart_parameters;
    }

    public function has_feature($str_feature_key) {

        $bln = false;

        switch($str_feature_key) {
        case Common_Chart::FEATURE_3D_VERSION:
        case Common_Chart::FEATURE_DATA_ROTATION:
        case Common_Chart::FEATURE_LEGEND_POSITION;
        case Common_Chart::FEATURE_COLOUR_PER_ITEM:
            $bln = true;
            break;
        }

        return $bln;
    }
    
}
